package Test32;

import java.util.LinkedHashSet;
import java.util.Objects;

public class Test18 {
    public static void main(String[] args) {
        //1、创建学生对象
        Student18 s1 = new Student18("zhangsan", 23);
        Student18 s2 = new Student18("lisi", 24);
        Student18 s3 = new Student18("wangwu", 25);
        Student18 s4 = new Student18("zhangsan", 23);

        //2、创建集合的对象
        LinkedHashSet<Student18> lhs = new LinkedHashSet<>();

        //3、添加数据
        System.out.println(lhs.add(s1));
        System.out.println(lhs.add(s2));
        System.out.println(lhs.add(s3));
        System.out.println(lhs.add(s4));

        //4、遍历集合
        //Lambda表达式
        lhs.forEach(stu -> System.out.println(stu.getName() + " " + stu.getAge()));
    }
}

class Student18 {
    private String name;
    private int age;


    public Student18() {
    }

    public Student18(String name, int age) {
        this.name = name;
        this.age = age;
    }

    /**
     * 获取
     *
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     *
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     *
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置
     *
     * @param age
     */
    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student18 student18 = (Student18) o;
        return age == student18.age && Objects.equals(name, student18.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    public String toString() {
        return "Student17{name = " + name + ", age = " + age + "}";
    }
}
